/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InstallmentsForm.java
 *
 * Created on Feb 9, 2013, 2:14:51 AM
 */
package vesko1;

import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import vesko1.model.Installments;
import vesko1.model.InsuranceDAO;

/**
 *
 * @author Nikolay
 */
public class InstallmentsForm extends javax.swing.JFrame {
    
    private int insId;
    private final int MAX_NUMBER_OF_INSTALLMENTS=4;

    /** Creates new form InstallmentsForm */
    public InstallmentsForm(String egn, int insuranceId, float totalSum, int numberOfPayments) {
        initComponents();
        
        this.setTitle("Настройка на разсрочено плащане за клиент с ЕГН : " + egn);
        this.insId = insuranceId;
        
        firstDate.setModel(new SpinnerDateModel(new Date(), null, null,
                Calendar.DAY_OF_MONTH));
        secondDate.setModel(new SpinnerDateModel(new Date(), null, null,
                Calendar.DAY_OF_MONTH));
        thirdDate.setModel(new SpinnerDateModel(new Date(), null, null,
                Calendar.DAY_OF_MONTH));
        fourthDate.setModel(new SpinnerDateModel(new Date(), null, null,
                Calendar.DAY_OF_MONTH));


        firstDate.setEditor(new JSpinner.DateEditor(firstDate, "yyyy-MM-dd"));
        secondDate.setEditor(new JSpinner.DateEditor(secondDate, "yyyy-MM-dd"));
        thirdDate.setEditor(new JSpinner.DateEditor(thirdDate, "yyyy-MM-dd"));
        fourthDate.setEditor(new JSpinner.DateEditor(fourthDate, "yyyy-MM-dd"));
        
        clientEgn.setText(egn);
        totalValue.setText(String.valueOf(totalSum) + " лв.");
        installmentsLabel.setText("Брой вноски :" + numberOfPayments);
        if (numberOfPayments < MAX_NUMBER_OF_INSTALLMENTS) {
            thirdDate.setVisible(false);
            fourthDate.setVisible(false);
            thirdInst.setVisible(false);
            fourthInst.setVisible(false);
            thirdPaymentLabel.setVisible(false);
            fourthPaymentLabel.setVisible(false);
        }
    }
    
    protected void processWindowEvent(WindowEvent e) {
            if (e.getID() == WindowEvent.WINDOW_CLOSING)
                dispose();
            else
                super.processWindowEvent(e);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        clientEgn = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        firstInst = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        thirdPaymentLabel = new javax.swing.JLabel();
        fourthPaymentLabel = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        thirdInst = new javax.swing.JTextField();
        secondInst = new javax.swing.JTextField();
        fourthInst = new javax.swing.JTextField();
        firstDate = new javax.swing.JSpinner();
        secondDate = new javax.swing.JSpinner();
        thirdDate = new javax.swing.JSpinner();
        fourthDate = new javax.swing.JSpinner();
        saveChangesButt = new javax.swing.JButton();
        installmentsLabel = new javax.swing.JLabel();
        dismissBut = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        totalValue = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(vesko1.Vesko1App.class).getContext().getResourceMap(InstallmentsForm.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        clientEgn.setFont(resourceMap.getFont("clientEgn.font")); // NOI18N
        clientEgn.setText(resourceMap.getString("clientEgn.text")); // NOI18N
        clientEgn.setName("clientEgn"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        firstInst.setText(resourceMap.getString("firstInst.text")); // NOI18N
        firstInst.setName("firstInst"); // NOI18N
        firstInst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                firstInstActionPerformed(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        thirdPaymentLabel.setText(resourceMap.getString("thirdPaymentLabel.text")); // NOI18N
        thirdPaymentLabel.setName("thirdPaymentLabel"); // NOI18N

        fourthPaymentLabel.setText(resourceMap.getString("fourthPaymentLabel.text")); // NOI18N
        fourthPaymentLabel.setName("fourthPaymentLabel"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        thirdInst.setName("thirdInst"); // NOI18N
        thirdInst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                thirdInstActionPerformed(evt);
            }
        });

        secondInst.setName("secondInst"); // NOI18N
        secondInst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                secondInstActionPerformed(evt);
            }
        });

        fourthInst.setName("fourthInst"); // NOI18N
        fourthInst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fourthInstActionPerformed(evt);
            }
        });

        firstDate.setName("firstDate"); // NOI18N

        secondDate.setName("secondDate"); // NOI18N

        thirdDate.setName("thirdDate"); // NOI18N

        fourthDate.setName("fourthDate"); // NOI18N

        saveChangesButt.setText(resourceMap.getString("saveChangesButt.text")); // NOI18N
        saveChangesButt.setName("saveChangesButt"); // NOI18N
        saveChangesButt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveChangesButtActionPerformed(evt);
            }
        });

        installmentsLabel.setText(resourceMap.getString("installmentsLabel.text")); // NOI18N
        installmentsLabel.setName("installmentsLabel"); // NOI18N

        dismissBut.setText(resourceMap.getString("dismissBut.text")); // NOI18N
        dismissBut.setName("dismissBut"); // NOI18N
        dismissBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dismissButActionPerformed(evt);
            }
        });

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        totalValue.setFont(resourceMap.getFont("totalValue.font")); // NOI18N
        totalValue.setText(resourceMap.getString("totalValue.text")); // NOI18N
        totalValue.setName("totalValue"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(190, 190, 190)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(170, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(installmentsLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(clientEgn)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(fourthPaymentLabel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(fourthInst, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel4)
                                        .addComponent(thirdPaymentLabel))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(thirdInst, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(secondInst, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(firstInst, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGap(18, 18, 18))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(secondDate, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(firstDate, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(thirdDate, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(fourthDate, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(dismissBut, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(saveChangesButt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(32, 32, 32))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addContainerGap()))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(totalValue)
                        .addGap(282, 282, 282))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(clientEgn)
                    .addComponent(jLabel9)
                    .addComponent(totalValue))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(installmentsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(firstDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(firstInst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(secondDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveChangesButt)
                    .addComponent(secondInst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(thirdPaymentLabel)
                    .addComponent(thirdDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dismissBut)
                    .addComponent(thirdInst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fourthPaymentLabel)
                    .addComponent(fourthDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fourthInst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void firstInstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_firstInstActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_firstInstActionPerformed

    private void thirdInstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_thirdInstActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_thirdInstActionPerformed

    private void secondInstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_secondInstActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_secondInstActionPerformed

    private void fourthInstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fourthInstActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fourthInstActionPerformed

    private void saveChangesButtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveChangesButtActionPerformed
        
        javax.swing.JTextField fields[] = {firstInst, secondInst, thirdInst, fourthInst};
        javax.swing.JSpinner dateFields[] = {firstDate, secondDate, thirdDate, fourthDate};

        ArrayList<Float> valuesList = new ArrayList<>();
        ArrayList<Date> datesList = new ArrayList<>();

        for ( int field=0; field < fields.length; field++ ) {
            if ( fields[field].getText().length() > 0 ) {
                valuesList.add(Float.valueOf(fields[field].getText()));
                datesList.add((Date)dateFields[field].getValue());
            }
        }
                
        InsuranceDAO.saveInstallments( new Installments(clientEgn.getText(), this.insId, 
                                        ((Float[])valuesList.toArray(new Float[valuesList.size()])), 
                                        ((Date[])datesList.toArray(new Date[datesList.size()]))));
    }//GEN-LAST:event_saveChangesButtActionPerformed

    private void dismissButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dismissButActionPerformed
        dispose();
    }//GEN-LAST:event_dismissButActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel clientEgn;
    private javax.swing.JButton dismissBut;
    private javax.swing.JSpinner firstDate;
    private javax.swing.JTextField firstInst;
    private javax.swing.JSpinner fourthDate;
    private javax.swing.JTextField fourthInst;
    private javax.swing.JLabel fourthPaymentLabel;
    private javax.swing.JLabel installmentsLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton saveChangesButt;
    private javax.swing.JSpinner secondDate;
    private javax.swing.JTextField secondInst;
    private javax.swing.JSpinner thirdDate;
    private javax.swing.JTextField thirdInst;
    private javax.swing.JLabel thirdPaymentLabel;
    private javax.swing.JLabel totalValue;
    // End of variables declaration//GEN-END:variables
}
